package kfs.light.core;

import java.awt.AWTException;
import java.awt.HeadlessException;
import java.util.List;
import java.util.Map;

import kfs.light.arduino.Arduino;
import kfs.light.screen.Color;
import kfs.light.screen.Screen;

public class Core extends Thread{
	public static Arduino arduinoModule;
	public static Screen screen;

	public static List<String> zoneList;

	public boolean stop = false;
	
	public  void run(){
		try {
			loop();
		} catch (HeadlessException | AWTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void exit(){
		stop=true;
	}
	
	public void loop() throws HeadlessException, AWTException, InterruptedException {		
		if (arduinoModule != null && screen != null && zoneList != null
				&& arduinoModule.isConnected()) {
			arduinoModule.start();
			while (!stop) {
				screen.getScreenShot();
				Map<String, Color> colorMap = screen.getColourMap();
				for (int i = 0; i < zoneList.size(); i++) {
					if (colorMap.containsKey(zoneList.get(i))) {
						arduinoModule.sendColor(i,
								colorMap.get(zoneList.get(i)));
					}
				}
			}
			arduinoModule.stop();
		}
	}
}
